package com.xiaoqing.framework;

import com.xiaoqing.framework.response.account.LoginResponse;
import com.xiaoqing.framework.service.LoginService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: <a href="get_xiaobo@163.com">Josh_Xu</a>
 * @description:
 * @create: 2020-06-19 22:32
 **/
@RestController
@RequestMapping("/account-login")
@Api(tags = "用户登录-Api")
@Slf4j
public class AccountEndpoint {

    private final LoginService loginService;

    @Autowired
    public AccountEndpoint(LoginService loginService) {
        this.loginService = loginService;
    }

    @ApiOperation(value = "用户登录", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, response = LoginResponse.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "服务端异常")
    })
    @GetMapping(value = "/login", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> login(@ApiParam(value = "用户名") @RequestParam(value = "accountName") String accountName,
                            @ApiParam(value = "密码") @RequestParam(value = "accountPwd") String accountPwd) {
        LoginResponse response = this.loginService.login(accountName, accountPwd);
        return ResponseEntity.ok(response);
    }


    @ApiOperation(value = "用户注册", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "服务端异常")
    })
    @GetMapping(value = "/register", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> register(@ApiParam(value = "用户名") @RequestParam(value = "accountName") String accountName,
                               @ApiParam(value = "密码") @RequestParam(value = "accountPwd") String accountPwd) {
        this.loginService.register(accountName, accountPwd);
        return ResponseEntity.ok().build();
    }


}
